package com.kungfuhacking.wristbandpro.location.activity;

import android.app.Dialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.TextView;

import com.amap.api.location.AMapLocationClient;
import com.amap.api.maps.MapView;
import com.amap.api.maps.model.BitmapDescriptor;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.MarkerOptions;
import com.kungfuhacking.wristbandpro.R;
import com.kungfuhacking.wristbandpro.base.view.BaseActivity;
import com.kungfuhacking.wristbandpro.custom.TitleBarView;
import com.kungfuhacking.wristbandpro.location.presenter.ISafeAddPresenter;
import com.kungfuhacking.wristbandpro.location.view.ISafeAddView;
import com.kungfuhacking.wristbandpro.utils.Constant;
import com.kungfuhacking.wristbandpro.utils.LogUtil;
import com.kungfuhacking.wristbandpro.widget.MySeekBar;


/**
 * 添加安全围栏页面
 * Created by Aaron on 17/5/8.
 */

public class SafeAreaAddActivity extends BaseActivity implements ISafeAddView, View.OnClickListener {

    private String TAG = getClass().getSimpleName();
    private TitleBarView tbv;
    private MapView mapView;
    private MySeekBar seekprogress;
    private android.widget.Button btn;
    private ISafeAddPresenter iSafeAddPresenter = new ISafeAddPresenter(this);
    private AMapLocationClient mLocationClient;
    protected boolean isTouchSeeked = false;
    private Dialog addSafeAreaDialog;

    /**
     * 安全区域的半径
     */
    private int mRadius;
    private EditText et_area_name;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_add_safe);

        mRadius = 100; //初始化半径为100m
        initWidget();
        mapView.onCreate(savedInstanceState);// 此方法必须重写
        initMap();

    }

    private void initMap() {
        mLocationClient = new AMapLocationClient(this);
        iSafeAddPresenter.initMap(mapView.getMap(), mLocationClient);
        iSafeAddPresenter.setupMap();
    }

    private void initWidget() {

        this.btn = (Button) findViewById(R.id.btn);
        this.seekprogress = (MySeekBar) findViewById(R.id.seek_progress);
        seekprogress.setMax(90);// 最大值90 因为有个默认的100
        this.mapView = (MapView) findViewById(R.id.gdmap);
        this.tbv = (TitleBarView) findViewById(R.id.tbv);
        btn.setOnClickListener(this);
        tbv.setOnLeftClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        /**
         * 进度拖动条监听器
         */
        seekprogress.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            // 通知进度已经被修改
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress,
                                          boolean fromUser) {
                if (isTouchSeeked) {
                    mRadius = progress * 10 + 100;
                    seekprogress.showSeekDialog(progress * 10 + 100 + "m");// 动态展示当前进度

                    LogUtil.d(TAG, "区域的半径==" + mRadius);

                    iSafeAddPresenter.addMarker();
                    iSafeAddPresenter.addCircle(mRadius);
                    iSafeAddPresenter.changeScal(mRadius);

                } else {
                    seekprogress.hideSeekDialog();
                }
            }

            // 通知用户已经开始一个触摸拖动手势
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                isTouchSeeked = true;
            }

            // 通知用户触摸手势已经结束
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                isTouchSeeked = false;
                seekprogress.hideSeekDialog();
            }
        });
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        if (mapView != null)
            mapView.onSaveInstanceState(outState);
    }

    @Override
    public void onResume() {
        super.onResume();
        if (mapView != null)
            mapView.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
        if (mapView != null)
            mapView.onPause();
    }

    private BitmapDescriptor des = null;

    @Override
    public MarkerOptions getMarket(LatLng latlng) {
        if (des == null) {
            Bitmap bMap = BitmapFactory.decodeResource(this.getResources(),
                    R.mipmap.ic_thumb);
            des = BitmapDescriptorFactory.fromBitmap(bMap);
        }
        MarkerOptions options = new MarkerOptions();
        options.icon(des);
        options.anchor(0.5f, 0.5f);
        options.position(latlng);
        return options;
    }

    @Override
    public int getRadius() {
        return mRadius;
    }

    @Override
    public String getSafeName() {
        return et_area_name.getText().toString();
    }

    @Override
    public void finishPage() {
        finish();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn:
                showAddDialog();
                break;

            case R.id.tv_cancle:
                if (addSafeAreaDialog != null && addSafeAreaDialog.isShowing())
                    addSafeAreaDialog.dismiss();
                break;

            case R.id.tv_create://创建安全区域

                if (iSafeAddPresenter.getmCurrentLatLng() == null)
                    return;

                iSafeAddPresenter.addSafeArea(sp.getString(Constant.CURRENTOLDID, ""),
                        iSafeAddPresenter.getmCurrentLatLng().latitude + "",
                        iSafeAddPresenter.getmCurrentLatLng().longitude + "");

                break;
        }
    }

    private void showAddDialog() {

        addSafeAreaDialog = new Dialog(this, R.style.no_title);
        addSafeAreaDialog.setCancelable(false);
        addSafeAreaDialog.setCanceledOnTouchOutside(false);
        addSafeAreaDialog.show();
        addSafeAreaDialog.setContentView(R.layout.dialog_add_safe_area);

        et_area_name = (EditText) addSafeAreaDialog.findViewById(R.id.et_area_name);
        TextView tv_create = (TextView) addSafeAreaDialog.findViewById(R.id.tv_create);
        TextView tv_cancle = (TextView) addSafeAreaDialog.findViewById(R.id.tv_cancle);
        tv_create.setOnClickListener(this);
        tv_cancle.setOnClickListener(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        iSafeAddPresenter.clearRequest();
    }
}
